package com.lucifer.cloud.auth.controller;
import com.lucifer.cloud.auth.entity.Oauth2BasicUser;
import com.lucifer.cloud.auth.entity.UserReq;
import com.lucifer.cloud.auth.model.Result;
import com.lucifer.cloud.auth.msg.producer.UserProducer;
import com.lucifer.cloud.auth.msg.producer.domain.MsgOauth2BasicUser;
import com.lucifer.cloud.auth.msg.producer.domain.MsgOauth2BasicUserConverter;
import com.lucifer.cloud.auth.service.IOauth2BasicUserService;
import com.lucifer.cloud.auth.support.RedisOperator;
import com.lucifer.cloud.auth.support.repeat.RepeatSubmit;
import jakarta.annotation.Resource;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import java.util.Objects;
import static com.lucifer.cloud.auth.constant.RedisConstants.SMS_CAPTCHA_FORGET_PREFIX_KEY;
import static com.lucifer.cloud.auth.constant.RedisConstants.SMS_CAPTCHA_REGISTER_PREFIX_KEY;

/**
 * @author lucifer
 * @date 2023/11/1 15:05
 */
@Slf4j
@RestController
@RequiredArgsConstructor
public class RegisterController {

    @Resource
    private IOauth2BasicUserService iOauth2BasicUserService;

    private final RedisOperator<String> redisOperator;
    @Resource
    private PasswordEncoder passwordEncoder;

    @Resource
    private UserProducer userProducer;


    @PostMapping("/register")
    @RepeatSubmit(expireSeconds = 6)
    public Result<String> register(
            @RequestBody UserReq registerUser
    ) {
        String phone = registerUser.getPhone();
        String code = registerUser.getCode();
        String smsCaptcha = redisOperator.get(SMS_CAPTCHA_REGISTER_PREFIX_KEY + phone);
        if(!Objects.equals(smsCaptcha,code)){
            return Result.error("短信验证码验证失败.");
        }
        Oauth2BasicUser basicUser = new Oauth2BasicUser();
        basicUser.setAccount(registerUser.getAccount());
        basicUser.setName(registerUser.getName());
        basicUser.setPassword(passwordEncoder.encode(registerUser.getPassword()));
        basicUser.setMobile(phone);
        basicUser.setEmail(registerUser.getEmail());
        basicUser.setDeleted(Boolean.FALSE);
        basicUser.setSourceFrom("system");
        if(iOauth2BasicUserService.registerByPhone(phone)){
            return Result.success("该手机号已经注册.", null);
        }
        boolean save = iOauth2BasicUserService.save(basicUser);
        if (save) {
            try {
                MsgOauth2BasicUser msgOauth2BasicUser = MsgOauth2BasicUserConverter.converterBasicUser2Msg(basicUser, null);
                userProducer.sendUser2Blog(msgOauth2BasicUser);
            } catch (Exception e) {
                log.error("人员注册给博客系统发送消息失败。message={}", e.getMessage());
            }
            return Result.success("注册成功.", null);
        } else {
            return Result.success("注册失败.", null);
        }
    }


    @PostMapping("/forget")
    @RepeatSubmit(expireSeconds = 3)
    public Result<String> forget(
            @RequestBody UserReq registerUser
    ) {
        String phone = registerUser.getPhone();
        String password = registerUser.getPassword();
        String code = registerUser.getCode();
        String smsCaptcha = redisOperator.get(SMS_CAPTCHA_FORGET_PREFIX_KEY + phone);
        if (!Objects.equals(smsCaptcha, code)) {
            return Result.error("短信验证码验证失败.");
        }
        Oauth2BasicUser basicUser = new Oauth2BasicUser();
        basicUser.setPassword(passwordEncoder.encode(password));
        basicUser.setMobile(phone);
        boolean update = iOauth2BasicUserService.updateByPhone(basicUser);
        if (update) {
            return Result.success("修改成功.", null);
        } else {
            return Result.success("修改失败.", null);
        }
    }

}
